home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / lib2 / sock_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-02  |  2.1 KB  |  83 lines

  1. /*    socket_init . c
  2. #
  3. %    Copyright (c)    Jin Guojun
  4. %
  5. %    Socket Initialization Server
  6. %
  7. % AUTHOR:    Jin Guojun - LBL    1/1/93
  8. */
  9.  
  10. #include "net_need.h"
  11.  
  12. #ifndef    SERVER
  13. #define    SERVER    "s_master"
  14. #endif
  15. #ifndef    SockWinFrag
  16. #define    SockWinFrag    64
  17. #endif
  18. #ifndef    DEF_PROTO
  19. #define    DEF_PROTO    "ip"
  20. #endif
  21.  
  22. static    struct    sockaddr_in    sain, sbin;    /* for binding only.    */
  23. static    int    protocol;
  24. static    struct    protoent* proto;
  25. static    char*    protoname = DEF_PROTO;
  26.  
  27. build_socket(char* host, char *serv_o_port, int socktype, bool duplex,
  28.     int* buf_size, struct sockaddr_in* bsin, magic_server*    ms)
  29. {
  30. int    so, trans = duplex < 0 ? ~duplex : duplex, /* ~-1 = 0, ~-2 = 1    */
  31.     udp = socktype==SOCK_DGRAM;
  32.  
  33.     if (!bsin)    bsin = &sain;
  34.     if (host && trans)
  35.         bsin->sin_family = get_addr(host, &bsin->sin_addr);
  36.     else    bsin->sin_family = AF_INET;
  37.     bsin->sin_port = get_port(serv_o_port ? serv_o_port : SERVER, udp);
  38.  
  39.     if (!proto && !(proto = getprotobyname(protoname)))
  40.         prgmerr(0, "protocol %s, 0 is used", protoname);
  41.     if (proto)    protocol = proto->p_proto;
  42.  
  43.     if ((so=socket(bsin->sin_family, socktype, protocol)) >= 0) {
  44.     if (ms)    {
  45.         ms->so = so;
  46.         ms->m_port = bsin->sin_port;
  47.     }
  48.     if (*buf_size > 0)    {
  49.         if (*buf_size > Max_WINDOW_SIZE)
  50.         *buf_size = Max_WINDOW_SIZE;
  51.         if (udp && *buf_size > UDP_BUF_LIMIT)
  52.         *buf_size = UDP_BUF_LIMIT;
  53.         while (setsockopt(so, SOL_SOCKET, trans ? SO_SNDBUF : SO_RCVBUF,
  54.             buf_size, sizeof(*buf_size) ) < 0)
  55.         if (*buf_size > SockWinFrag)    *buf_size -= SockWinFrag;
  56.         else
  57.     buferr:        return    prgmerr(0, "sockbuf size %d", *buf_size);
  58.         if (duplex < 0 &&
  59.         setsockopt(so, SOL_SOCKET, trans ? SO_RCVBUF : SO_SNDBUF,
  60.             buf_size, sizeof(int) ) < 0)    goto    buferr;
  61.     }
  62.  
  63.     if (trans)            /* bind any way.    */
  64.         sbin.sin_port = 0;    /* to get free port choice.    */
  65.     else    sbin.sin_port = bsin->sin_port;
  66.     if (socktype < SOCK_RAW && bind(so, &sbin, sizeof(sbin)) < 0)
  67.         close(so),    so = prgmerr(0, "sock_bind");
  68.     }
  69. return    so;
  70. }
  71.  
  72.  
  73. struct sockaddr_in    *
  74. get_soaddr(int anb, struct sockaddr_in *saddr)
  75. {
  76. struct sockaddr_in *sad;
  77. if (anb)    sad = &sain;
  78. else    sad = &sbin;
  79. if (saddr)    *saddr = *sad;
  80. return    sad;
  81. }
  82.  
  83.